// Custom theme scripts for GJW Titmuss
// Author:  J.Dennis
// Date:    21/03/13

////////////////////////////////////////////////////////////////////////////////////////////////////////
$(document).ready(function() {
	
	//home full-width banner flexslider
	$('#banner').flexslider({
		animation: "slide",
		controlNav: false,
		nextText: "",
		prevText: "",
		directionNav: true
	});
	
	//brand flexslider
	//brand flexslider
    $('.brand_slide').css('visible','hidden');
    setTimeout(function () {
    	$('.brand_slide').flexslider({
    		animation: "slide",
    		animationLoop: false,
    		slideshow: false,
    		itemWidth: 60,
    		itemHeight: 42,
    		minItems: 8,
    		maxItems: 8,
    		controlNav: false,
    		directionNav: true,
    		nextText: "More brands"
    	});
    	setTimeout(function () { $('.brand_slide').css('visible', 'visible'); }, 300);
    }, 250);
	
	//category flexslider
	$('#catBanner, #slider').flexslider({
		animation: "slide",
		controlNav: true,
		directionNav: true
	});

});	


// Megamenu script with overlay
$(function () {
	
	$('ul.productNav').children('li').hoverIntent({
     over: showMenu, 
     timeout: 100, 
     out: hideMenu
	});
		function showMenu(){
			var docHeight = $(document).height();
			var docWidth = $(document).width();
			$(".stickyHeader").append("<div class='overlay'></div>");
			$(".overlay").css({
				"height": docHeight,
				"width": docWidth,
				"z-index": 110
			}).fadeIn(500);
			$('ul.productNav').find('.megamenu').stop(true,true).hide();
			$(this).children("a").addClass("active");
			$('.miniBasket').css({"z-index": 0})
			$(this).find('.megamenu').fadeIn(500).css({
				"z-index": 210
			});			
		}
		function hideMenu(){
		 	$('ul.productNav').find('.megamenu').stop(true, true).hide();
			$(this).children("a").removeClass("active");				
			$(this).find('.megamenu').fadeOut(500);
			$(".overlay").delay(1000).fadeOut(500).remove();
		}

});


$(function() {
  var allPanels = $('.accordion > dd').hide();
  $('.accordion > dd.active').show();
  $('.accordion > dt > a').click(function() {
    $(this).parent().next().slideToggle();
    return false;
  });
})

$(function () {
	//Slide content
    //hide slide content
	$(".slide_content, .mb_panel, .hide").hide();
	//slideDown panel toggle
	$("a.slide_down").click(function (e) {
	    
		var panel_ID = $(this).parent("li").attr("class");
		
		if($(".slide_content").hasClass('active')){
			$('.slide_content').slideUp(200).delay(500).removeClass("active");
			$(".overlay").delay(1000).fadeOut(500).remove();
		}
		else{
			$("#" + panel_ID).css({"z-index": 500}).slideDown('slow').addClass("active");
			$("body").append("<div class='overlay'></div>");
			var docHeight = $(document).height();
			var docWidth = $(document).width();
			$(".overlay").css({
				"height": docHeight,
				"width": docWidth,
				"z-index": 100
			}).fadeIn(500);
		}
		return false;
	});
	
//	$(".view_mb").click(function (e) {
//		var panel_ID = $(this).parent("li").attr("class");
//		if($(".slide_content").hasClass('active')){
//			$('.slide_content').slideUp(200).delay(500).removeClass("active");
//			$('.miniBasket').css({"z-index": 0})
//			$(".overlay").delay(1000).fadeOut(500).remove();
//		}
//		else{
//			$("#" + panel_ID).css({"z-index": 500}).slideDown('slow').addClass("active");
//			$('.miniBasket').css({"z-index": 500})
//			$(".stickyHeader").append("<div class='overlay'></div>");
//			var docHeight = $(document).height();
//			var docWidth = $(document).width();
//			$(".overlay").css({
//				"height": docHeight,
//				"width": docWidth,
//				"z-index": 200
//			}).fadeIn(500);
//		}
//		return false;
//	});
	
	$("a.slide_down_b").click(function (e) {
		var panel_ID = $(this).parent("li").attr("class");
		if($(".slide_content").hasClass('active')){
			$('.slide_content').slideUp(200).delay(500).removeClass("active");
			$('.stickyfooter').css({"z-index": 0})
			$(".overlay").delay(1000).fadeOut(500).remove();
		}
		else{
			$("#" + panel_ID).css({"z-index": 500}).slideDown('slow').addClass("active");
			$('.stickyfooter').css({"z-index": 500})
			$("body").append("<div class='overlay'></div>");
			var docHeight = $(document).height();
			var docWidth = $(document).width();
			$(".overlay").css({
				"height": docHeight,
				"width": docWidth,
				"z-index": 200
			}).fadeIn(500);
		}
		return false;
	});
	
	
	$(".slide_content").hoverIntent({
     over: show,
     timeout: 200,
     out: hide
	});
	function show(){}
	function hide(){
		$('.slide_content').slideUp(200).removeClass("active");
		$(".overlay").delay(1000).fadeOut(500).remove();
		}
	
	$("#mainBody, .banner, .close").click(function () {	
		 if ($(".slide_content").is(':visible')){
			$(".slide_content").slideUp("fast").removeClass("active");
		 }
		 else{}
	});

});

//equal height function
function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

	
////////////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function() {
	
	//image error - "no photo"
		//all none specific images
		$('img').error(function(){
			$(this).attr('src', 'http://content.gjwtitmuss.co.uk/1/1/images/icons/no-image-300x300.gif');
		});
		//small prod images
		$('.prodImg .small, img.small').error(function(){
			$(this).attr('src', 'http://content.gjwtitmuss.co.uk/1/1/images/icons/no-image-70x70.gif');
		});
		//med prod images
		$('.prodImg .med,img.med').error(function(){
			$(this).attr('src', 'http://content.gjwtitmuss.co.uk/1/1/images/icons/no-image-150x150.gif');
		});
		//large prod images
		$('.prodImg .large,img.large').error(function(){
			$(this).attr('src', 'http://content.gjwtitmuss.co.uk/1/1/images/icons/no-image-300x300.gif');
		});
		
	//remove special offer if empty
	$('.special_offer p:empty').parent('.special_offer').remove();
	
	// initiate popup
	$("a[rel^='prettyPopin']").prettyPopin();
	
	// delivery addresss hide/show
	$('#deliveryAddress_form').hide();
	$('#show_delivery_address').change(function(){
		if(this.checked){
			$('#deliveryAddress_form').fadeOut('slow');
		}
		else{
			$('#deliveryAddress_form').fadeIn('slow');
		}
	});
	
	//jquery datepicker
    $('.datepicker').datepicker();

	//deliver options show/hide
	$('input:radio[name="delivery"]').change(function(){
        if ($(this).val() == 'deliver') {
            $('#delivery_options').fadeIn('slow');
        }
        else {
            $('#delivery_options').fadeOut('slow');
        }
    });	
	

	//prettyPhoto
	$("a[rel^='prettyPhoto']").prettyPhoto({});
	//prettyGallery
	$("ul.gallery").prettyGallery({
		'navigation':'bottom',
		'itemsPerPage':4
	});
	
	//photoswitcher
	$("ul.gallery li.img a").click(function(e){
		var prodPic = $(this).attr('href');
		console.log(prodPic);
		$('ul.gallery li').removeClass('active');
		$(this).parent('li').addClass('active');
		$(".prodImg-main a").attr('href', prodPic);
		$(".prodImg-main a img").attr('src', prodPic);
		return false;
	});
	
	//Equal height columns
	equalHeight($("#static_footer article"));
	equalHeight($(".eHeight"));
	equalHeight($(".adHeight"));
	//equalHeight($("#product-array.grid article.summary.stamp"));
	
	//dotdotdot text shortner
	$('.description.short, h1.prodName').dotdotdot({
		ellipsis	: '... ',
		wrap		: 'word', 
		fallbackToLetter: true,
		after		: null
	});
	
	//show hidden content
	$("a.show_btn").click(function (e) {
	    var show_ID = $(this).attr("id");
	    $(".panel").addClass("hide").hide();
		$("." + show_ID).removeClass("hide").fadeIn("fast");
	});

    ////////////////////////////////////////////////////////////////////////////////////////////////////////

    //Trustpilot JSON feed function
	$.ajax({  
	    url: 'http://s.trustpilot.com/tpelements/702177/f.jsonp',
	    dataType: 'jsonp',
		jsonp: false,
  		jsonpCallback: 'trustpilot_jsonp_callback',
	    success: function(data) { 
		    $('.tp_bar').append( data.TrustScore.Human+' - <span class="lowercase">' + data.TrustScore.Score + '/100 - <img src="' + data.TrustScore.StarsImageUrls.small + '" /></span>'); 
			//$('.tp_bar').append( data.TrustScore.Human+' - <span class="lowercase">' + data.TrustScore.Score + ' | <img sr="' + data.TrustScore.StarsImageUrls.small + '" /> | ' + data.ReviewCount.Total + ' reviews</span>');
	    }
	});

    ////////////////////////////////////////////////////////////////////////////////////////////////////////

	$("section#breadcrumbs a.here").click(function (e) { });

    //Product view Grid/List Change 
    $(".product_views li a").click(function (e) {
        var viewId = $(this).attr("class");
        $("#product-array").fadeOut(500).hide().removeClass().addClass(viewId);
        $("#product-array").fadeIn(500);
        $(".product_views li").removeClass("active");
        $(this).parent("li").addClass("active");
	    return false;
    });

	//filter expand/contract - header
    $(".result_filters header, .category_infobar header, .hsPanel header").click(function () {
		$(this).next('.panel_content').slideToggle();
		if($(this).hasClass('min')){
			$(this).removeClass('min');
		}
		else{
			$(this).addClass('min');
		};
    });
	
	//filter expand/contract - footer
    $(".result_filters footer, .category_infobar footer, .hsPanel footer").click(function () {
		$(this).prev('.panel_content').slideToggle();
		if($(this).hasClass('min')){
			$(this).removeClass('min');
		}
		else{
			$(this).addClass('min');
		};
    });
	
	//Expandable content panels
	$('.result_filters.close header, .category_infobar.close header, .hsPanel.close header, .result_filters.close footer, .category_infobar.close footer, .hsPanel.close footer').addClass('min');
	$('.result_filters.close .panel_content, .category_infobar.close .panel_content, .hsPanel.close .panel_content, .result_filters.close .panel_content, .category_infobar.close .panel_content, .hsPanel.close .panel_content').hide();
	

	////////////////////////////////////////////////////////////////////////////////////////////////////////

	//Tabs
	$('#tabs div.whiteBox').hide();
	$('#tabs div.whiteBox:first').show();
	$('#tabs ul li:first').addClass('active');

	$('#tabs ul.tabHeader li a').click(function () {
		$('#tabs ul li').removeClass('active');
		$(this).parent().addClass('active');
		var currentTab = $(this).attr('href');
			$('#tabs .whiteBox').hide();
			$(currentTab).fadeIn(100);
		return false;
	});
	
	$('#tabs.fixedHeight ul.tabHeader  li a').click(function () {
		$('#tabs.fixedHeight ul li').removeClass('active');
		$(this).parent().addClass('active');
		
		var currentTab = $(this).attr('href');
		
			$('.contentwindow').removeClass('nano');
			$('.contentwindow').removeClass('has-scrollbar');
			
			$('#tabs.fixedHeight .whiteBox').hide();
			
			$(currentTab).addClass('nano');
			$(currentTab).addClass('has-scrollbar');	
			
			$(currentTab).fadeIn(100);
			$(currentTab).nanoScroller();
			
		return false;
	});

	//Initiate NanoScroller	
	$("#tabs.fixedHeight .nano, tabs.fluid .nano, .prodInfo_window.nano").nanoScroller();

	//Initiate umber stepper for Qtys
	//$("input[type='number']").stepper();

	////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	//Category Tree	
	$(".tree .more").click(function () {
		$(this).children('ul').slideToggle();
		
		if($(this).hasClass('min')){
			$(this).removeClass('min').addClass('max');
		}
		else{
			$(this).addClass('min').removeClass('max');
		};
		
	});
	
	//expand prod window to show all brands
	$(".prodInfo_window_tools .btn").click(function () {
		var catcontainer = $(this).parent().parent().parent().attr('id');
		var catTreeHeight = $(this).parent().parent().find(".prodInfo_window .content ul").height();
		
		if($(this).hasClass('max')){
			$("#"+catcontainer + " .prodInfo_window").animate({ height: catTreeHeight }, 200);
			$(this).addClass('hide').hide();
			$("#"+catcontainer + " .btn.min").fadeIn(500).removeClass('hide');
		}
		else{
			$("#"+catcontainer + " .prodInfo_window").animate({ height: 200 }, 200);
			$(this).addClass('hide').hide();
			$("#"+catcontainer + " .btn.max").fadeIn(500).removeClass('hide');
		};
	});
	
	//Change tree content upon list selection
	$("#cat_select").change(function () {
		var cat_selection = $(this).val();
		$(".tree").slideUp(100).removeClass('active');   
		$("#" + cat_selection + ".tree").slideDown(100).addClass('active');
	})
	.trigger('change')
	
	//Change brandslide content upon list selection
	$("#brand_select").change(function () {
		var brand_selection = $(this).val();
		$(".brandslides").fadeOut(50).delay(100).removeClass('active');
		$("#" + brand_selection + ".brandslides").fadeIn(200).addClass('active');
	})
	.trigger('change')
	
	
	//Change checkout proceed btn
	$("input[name$='payment']").click(function() {
        var payment = $(this).val();
		$('.ajaxLoadWindow').empty();
		$("a.proceed_btn").hide().delay(100).removeClass('active');
		$("#" + payment).fadeIn(200).addClass('active');
    });
	
	
	//Ajax call for card details	
	$.ajaxSetup({cache: false});
    var ajax_load = "";
    
	$("a.ajaxCall").click(function(e) {
		var loadUrl = $(this).attr('href');
		console.log(loadUrl);
		var loadIntoWindow = $(this).attr('rel');
		console.log(loadIntoWindow);
		$("#"+ loadIntoWindow +".ajaxLoadWindow").html(ajax_load).load(loadUrl);
    });
	
	//unload ajax
	$("#cancelButton").click(function(e) {
		$('.ajaxLoadWindow').empty();
		return false;
	});
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
});